<?php
define("THIS_PAGE", "invoices");

require_once($_SERVER["DOCUMENT_ROOT"] . "/../sys/libs/init.lib");
include($_SERVER["DOCUMENT_ROOT"] . "/../sys/includes/adminheader.php");

print "<div class=\"pagebody\">";

$SQL = "SELECT c.BusinessName AS Client, \n"
	 . "        ROUND(SUM(inv.ItemValue),2) AS InvoiceValue, i.id AS InvoiceID, i.DueDate, i.Date, SUM(IFNULL(pay.Payment, 0)) AS PaymentTotal, COUNT(ii.id) AS InvoiceItemCount \n"
	 . "  FROM invoice i \n"
	 . "       JOIN (SELECT (SUM(IFNULL(AmountEach, 0)) + SUM(IFNULL(TaxAmount, 0)) * IFNULL(Quantity, 1)) AS ItemValue, InvoiceID \n"
	 . "               FROM item_invoice \n"
	 . "              GROUP BY InvoiceID) inv ON inv.InvoiceID = i.id \n"
	 . "       LEFT JOIN (SELECT ROUND(SUM(p.Amount), 2) AS Payment, pi.InvoiceID \n"
	 . "                    FROM payment p \n"
	 . "                         JOIN payment_invoice pi ON pi.PaymentID = p.id \n"
	 . "                   GROUP BY pi.InvoiceID) pay ON pay.InvoiceID = i.id \n"
	 . "       JOIN clients c ON c.id = i.ClientID \n"
	 . "       JOIN item_invoice ii ON ii.InvoiceID = i.id \n"
	 . "       LEFT JOIN payment_invoice pi ON pi.InvoiceID = i.id \n"
	 . "       LEFT JOIN payment p ON p.id = pi.PaymentID \n"
	 . " GROUP BY i.id \n";

$invoices = $db->getarray($SQL);

print "<table width=\"100%\" border=\"0\" cellspacing=\"0\">
		<tr>
			\t<th align=\"left\">Date Issued</th>
			\t<th align=\"left\">Client</th>
			\t<th align=\"left\">Due Date</th>
			\t<th align=\"left\">Items</th>
			\t<th align=\"left\">Total Value</th>
			\t<th align=\"right\">Total Payments</th>
			\t<th align=\"right\">Action</th>
		</tr>\n";

$row1 = "#ffffff";
$row2 = "#9999ff";

$i = 0;

$js[] = "
	function init() {
		myPanel = new YAHOO.widget.Panel('myPanel', { width:'150px', visible:false, constraintoviewport:true, fixedcenter:true } );
		myPanel.render();
	}

	YAHOO.util.Event.addListener(window, 'load', init);

	function showPanel() {
		myPanel.show();
	}
";

foreach ($invoices as $key => $invoice) {

	$bgcolor = ($i++ % 2) ? $row1 : $row2;

	print "<tr style=\"background-color:{$bgcolor};\" id=\"{$invoice->InvoiceID}\">
			\t<td>{$invoice->Date}</td>
			\t<td>{$invoice->Client}</td>
			\t<td>{$invoice->DueDate}</td>
			\t<td>{$invoice->InvoiceItemCount}</td>
			\t<td>{$invoice->InvoiceValue}</td>
			\t<td>{$invoice->PaymentTotal}</td>
			\t<td align=\"right\"><img src='./img/edit_add.png' alt='Log payment' name='logPayment_{$invoice->InvoiceID}' id='logPayment_{$invoice->InvoiceID}' title='Log Payment' />
                                  <a href='admin_invoices_edit.php?invoiceid={$invoice->InvoiceID}'><img src='./img/edit.png' alt='Edit Invoice' title='Edit Invoice' border='0' /></a>
                                  <img src='./img/delete.png' /></td>
			</tr>\n";

	$js[] = "YAHOO.util.Event.addListener('logPayment_{$invoice->InvoiceID}', 'click', showPanel);\n";

}

?>
</table></form>
<?php
	include("admin_payment_new.php");
	include($_SERVER["DOCUMENT_ROOT"] . "/../sys/includes/adminfooter.php");
?>